package com.qdsg.ysg.doctor.pharmacist.activity;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.CheckResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 已审核列表
 *
 * @time 2020/12/1 17:22
 */
public class PrescriptionCheckedActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    MyAdapter adapter;

    List<CheckResponse.Check> list = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_prescription_checked;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager( this );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        adapter = new MyAdapter();
        recyclerView.setAdapter( adapter );
        recyclerView.addItemDecoration( new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.onDraw( c, parent, state );
            }

            @Override
            public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                super.getItemOffsets( outRect, view, parent, state );
                outRect.bottom = DpUtil.dip2px( PrescriptionCheckedActivity.this, 1 );
            }
        } );

        refreshLayout.setEnableLoadMore( false );
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> initData());
    }

    @Override
    protected void initData() {
        getCheckList();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NotNull
        @Override
        public MyAdapter.Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder( LayoutInflater.from( mContext ).inflate( R.layout.item_im, parent, false ) );
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener( v -> {
                Bundle bundle = new Bundle();
                bundle.putSerializable( "obj", list.get( position ) );
                startActivity( PresciptionDetailActivity.class, bundle );
            } );
            holder.tv_name.setText( list.get( position ).strType + "处方" );
//            holder.tv_department.setText(list.get(position).deptName);
            holder.tv_message.setText( list.get( position ).docName );
            holder.tv_time.setText( list.get( position ).createTime );
//            if(list.get(position).unreadCount!=0){queryPrescription
//                holder.tv_unreadCount.setVisibility(View.VISIBLE);
//                holder.tv_unreadCount.setText(list.get(position).unreadCount+"");
//            }else {
            holder.tv_unreadCount.setVisibility( View.GONE );
//            }
            ImageLoaderHelper.getInstance().GlideImageLoader( mContext, list.get( position ).url, holder.img_head, R.mipmap.img_default );
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_department, tv_message, tv_time, tv_unreadCount;
            ImageView img_head;

            Holder(View itemView) {
                super( itemView );
                tv_name = itemView.findViewById( R.id.tv_name );
                tv_unreadCount = itemView.findViewById( R.id.tv_unreadCount );
                tv_time = itemView.findViewById( R.id.tv_time );
                tv_department = itemView.findViewById( R.id.tv_department );
                tv_message = itemView.findViewById( R.id.tv_message );
                img_head = itemView.findViewById( R.id.img_head );
            }
        }
    }

    private void getCheckList() {
        RestProxy.getInstance().getCheckList( "1", "", new Observer<CheckResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CheckResponse checkResponse) {
                list.clear();
                list.addAll( checkResponse.data.records );
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                refreshLayout.finishRefresh( false );
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh( true );
            }
        } );
    }
}

